﻿

#region Using directives
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using InvoiceChecking.Data;
using InvoiceChecking.Entities;
using InvoiceChecking.Web.UI;
#endregion

public partial class OldPaymentPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FormUtil.RedirectAfterUpdate(GridView1, "Payment.aspx?page={0}");
        FormUtil.SetPageIndex(GridView1, "page");
        FormUtil.SetDefaultButton((Button)GridViewSearchPanel1.FindControl("cmdSearch"));
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string urlParams = string.Format("PaymentId={0}", GridView1.SelectedDataKey.Values[0]);
        Response.Redirect("PaymentEdit.aspx?" + urlParams, true);
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (ddlAction.SelectedValue == "Print")
        {
            var payments = GetSelectedRow();
            if (payments.Count > 0)
            {
                Session["Payment"] = payments;
                Response.Redirect("ReportPaymentVoucher.aspx", true);
            }
        }
        else if (ddlAction.SelectedValue == "Close")
        {
            var payments = GetSelectedRow();
            payments = payments.FindAll("IsCompleted", false);
            foreach (var payment in payments)
            {
                payment.IsCompleted = true;
                DataRepository.PaymentProvider.Update(payment);
            }
            GridView1.DataBind();
        }
        else if (ddlAction.SelectedValue == "Pay")
        {
            var payments = GetSelectedRow();
            payments = payments.FindAll("PaidBy", null);
            foreach (var payment in payments)
            {
                PayPayment(payment.PaymentId);
            }
            GridView1.DataBind();
        }
    }


    private TList<Payment> GetSelectedRow()
    {
        var payments = new TList<Payment>();
        foreach (GridViewRow row in GridView1.Rows)
        {
            var cbCheck = (CheckBox)row.FindControl("cb_Check");
            if (cbCheck.Checked)
            {
                var paymentId = GridView1.DataKeys[row.RowIndex].Value.ToString();
                var payment = DataRepository.PaymentProvider.GetByPaymentId(paymentId);
                payments.Add(payment);
            }
        }
        return payments;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Pay")
        {
            var paymentId = e.CommandArgument.ToString();
            PayPayment(paymentId);
            GridView1.DataBind();
        }
    }

    private void PayPayment(string paymentId)
    {
        var payment = DataRepository.PaymentProvider.GetByPaymentId(paymentId);
        DataRepository.PaymentProvider.DeepLoad(payment, true);
        var paymentAccount =
            DataRepository.PaymentAccountProvider.GetByPaymentAccount(payment.PaymentTypeIdSource.AccountNumber);
        if (paymentAccount != null && payment.PaidDate == null)
        {
            payment.PaidBy = Context.User.Identity.Name.Split('\\')[1];
            payment.PaidDate = DateTime.Now;
            decimal Amount = 0;
            foreach (var paymentDetail in payment.PaymentDetailCollection)
            {
                //Payment by VND
                if (payment.PaymentTypeIdSource.Currency == "VND")
                {
                    if (paymentDetail.MoneyType == "VND")
                        if (paymentDetail.DebitCredit == "Credit")
                            Amount += paymentDetail.Amount.Value;
                        else
                            Amount -= paymentDetail.Amount.Value;
                    else
                    {
                        if (paymentDetail.DebitCredit == "Credit")
                            Amount += paymentDetail.Amount.Value * payment.ExchangeRate.Value;
                        else
                            Amount -= paymentDetail.Amount.Value * payment.ExchangeRate.Value;
                    }
                }
                else //Payment by USD
                {
                    if (paymentDetail.MoneyType == "USD")
                        if (paymentDetail.DebitCredit == "Credit")
                            Amount += paymentDetail.Amount.Value;
                        else
                            Amount -= paymentDetail.Amount.Value;
                    else
                    {
                        if (paymentDetail.DebitCredit == "Credit")
                            Amount += paymentDetail.Amount.Value / payment.ExchangeRate.Value;
                        else
                            Amount -= paymentDetail.Amount.Value / payment.ExchangeRate.Value;
                    }
                }
            }
            payment.OpenBalance = paymentAccount.CurrentBalance;
            if (Amount > 0)
                payment.IsDebit = false;
            else
                payment.IsDebit = true;
            payment.Amount = Math.Abs(Math.Round(Amount, 2));
            paymentAccount.CurrentBalance -= Amount;
            payment.IsCompleted = true;
            DataRepository.PaymentAccountProvider.Update(paymentAccount);
            DataRepository.PaymentProvider.Update(payment);
        }
    }
}


